package com.lepu.pcecg500.util;

import android.content.Context;
import android.os.Environment;

import net.tsz.afinal.FinalDb;
import net.tsz.afinal.db.sqlite.DbModel;

import java.util.List;

/**
 * 这个类是操作PDA info.db 数据库文件
 */
public class FinalDbManager {

	// 数据库存放的文件夹
	private static String pathStr = Environment.getExternalStorageDirectory() + Constant.DB_PATH;
	private static FinalDb infoDb;

	private FinalDbManager() {
	}

	private static class LazyHolder {
		private static final FinalDbManager INSTANCE = new FinalDbManager();
	}


	public static final FinalDbManager getInstance(Context context) {
		infoDb = infoDb == null ? FinalDb.create(context, pathStr,"info.db") : infoDb;
		return LazyHolder.INSTANCE;
	}

	public void saveInInfoDB(Object obj) {
		infoDb.save(obj);
	}

	public <T> List<T> findAllInInfoDB(Class<T> clazz) {

		return infoDb.findAll(clazz);
	}

	public <T> List<T> findAllInInfoDB(Class<T> clazz, String orderBy) {
		return infoDb.findAll(clazz, orderBy);
	}

	public <T> List<T> findAllByWhereInInfoDB(Class<T> clazz, String strWhere) {
		return infoDb.findAllByWhere(clazz, strWhere);
	}

	public <T> List<T> findAllByWhereInInfoDB(Class<T> clazz, String strWhere, String orderBy) {
		return infoDb.findAllByWhere(clazz, strWhere, orderBy);
	}

	public <T> Object findByIdInInfoDB(Object id, Class<T> clazz) {
		return infoDb.findById(id, clazz);
	}

	public DbModel findDbModelBySQLInInfoDB(String strSQL) {
		return infoDb.findDbModelBySQL(strSQL);
	}

	public List<DbModel> findDbModelListBySQLInInfoDB(String strSQL) {
		return infoDb.findDbModelListBySQL(strSQL);
	}

	public void deleteInInfoDB(Object entity) {
		infoDb.delete(entity);
	}

	public void deleteByWhere(Class clazz, String strWhere) {
		infoDb.deleteByWhere(clazz,strWhere);
	}

	public void updataInInfoDB(Object entity) {
		infoDb.update(entity);
	}

	public void updataInInfoDB(Object entity, String strWhere) {
		infoDb.update(entity, strWhere);
	}

	public void save(Object entity) {
		infoDb.save(entity);
	}


}